<!DOCTYPE html>
<html>
<head>
  <link rel="chrome-webstore-item">
</head>
<body>
<script>
  function runTest(galleryUrl) {
    // Link URL has to be generated dynamically in order to include the right
    // port number. The ID corresponds to the data in the "extension" directory.
    document.getElementsByTagName('link')[0].href =
        galleryUrl + '/detail/ecglahbcnmdpdciemllbhojghbkagdje';

    try {
      chrome.webstore.install(
          undefined,
          function() {},
          function() {});
      chrome.webstore.install(
          undefined,
          function() {
            console.log('Unexpected success');
            window.domAutomationController.send(false);
          },
          function(errorMessage, errorCode) {
            var success = errorCode == 3;  // Install in progress error.
            if (!success)
              console.log('Unexpected error: ' + errorMessage);
            window.domAutomationController.send(success);
          });
    } catch (e) {
      // Yuck. We can throw this "already installing" error either from the
      // renderer (if we catch the fact that it's already installing early) or
      // from the browser. In the first case, it's a thrown error, and in the
      // second, it triggers the callback from chrome.webstore.install. And to
      // top it off, they're different messages. Unfortunately, it's hard to say
      // which, if either, we can change, since someone may be relying on one or
      // the other.
      // TODO(devlin): Make this less bad somehow. crbug.com/624614
      var success =
          e.message == 'A Chrome Web Store installation is already pending.';
      if (!success)
        console.log('Unexpected exception: ' + e);
      window.domAutomationController.send(success);
    }
  }
</script>

</body>
</html>
